package com.edu.service.impl;

import com.edu.bean.User;
import com.edu.mapper.UserMapper;
import com.edu.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:max
 * @Date: 2024/11/8 16:56
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public int addUser(User user) {
        return userMapper.addUser(user);
    }

    @Override
    public int deleteUser(int userId) {
        return userMapper.deleteUser(userId);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public User selectUserById(int userId) {
        return userMapper.selectUserById(userId);
    }

    @Override
    public List<User> selectAllUser() {
        return userMapper.selectAllUser();
    }

    @Override
    @Transactional
    public int transferMoney(int userId1, int userId2, double money) {
        User user1=userMapper.selectUserById(userId1);
        User user2 = userMapper.selectUserById(userId2);
        if(user2==null ){
            return -2;
        }
        if(user1.getMoney()<money){
            return -1;
        }
        int r1=userMapper.decreaseMoney(userId1,money);
        int r2=userMapper.increaseMoney(userId2,money);
        return r1==1 && r2==1 ? 1 : 0;
    }
}
